package com.crud.project.controller;

import com.crud.project.po.User;
import com.crud.project.service.UserService;
import com.crud.project.util.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/main")
    public String findAllUser(@RequestParam(required=true,defaultValue="1") Integer page, Model model){
        PageHelper.startPage(page,3);
        List<User> userList=userService.findAllUser();
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        model.addAttribute("user",pageInfo);
        return  "first";
    }

    @PostMapping("/main/search")
    public String search(@RequestParam(required=true,defaultValue="1") Integer page,@RequestParam("username") String username, Model model){
        if (username==null){
            PageHelper.startPage(page,3);
            List<User> userList=userService.findAllUser();
            PageInfo<User> pageInfo=new PageInfo<>(userList);
            model.addAttribute("user",pageInfo);
            return "first ::blogList";
        }
        PageHelper.startPage(page,3);
        List<User> userList=userService.findAllUserName(username);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        model.addAttribute("user",pageInfo);
        return "first ::blogList";
    }

    @GetMapping("/second")
    public String second(Model model,HttpSession httpSession){
        if (httpSession.getAttribute("user")==null){
            model.addAttribute("user",new User());
        }
        return "second";
    }

    @PostMapping("/insertUser")
    public String insertUser(User user, RedirectAttributes redirectAttributes){
            user.setUser_create_time(new Date());
        List<User> userList=userService.findAllUser();
        for (User user1:userList){
            if (user1.getUser_real_name().equals(user.getUser_real_name())){
                redirectAttributes.addFlashAttribute("message","用户已存在，不可以重复提交相同数据");
                System.out.println("用户已存在，不可以重复提交相同数据");
                return "redirect:/main";
            }
            if (user.getUser_name()==null&&user.getUser_password()==null){
                String user_name="",user_password="";
                for (int i = 0; i < 3; i++) {
                    user_name = user_name + (int) (Math.random() * 10);
                    user_password = user_password + (int) (Math.random() * 20);
                }
                user.setUser_name(user_name);
                user.setUser_password(MD5Utils.code(user_password));
            }
        }
        userService.insertUser(user);
        return "redirect:/main";
    }

    @GetMapping("/delete/{id}")
    public String deleteById(@PathVariable Long id){
        userService.deleteUserById(id);
        return "redirect:/main";
    }

    @GetMapping("/updateUser/{id}")
    public String updateUserById(@PathVariable Long id,Model model){
         User user=userService.findById(id);
         model.addAttribute("user",user);
        return "second";
    }

    @PostMapping("/update/{id}")
    public String update(@PathVariable Long id,User user){
        user.setUser_create_time(new Date());
        User user1=userService.findById(id);
        user.setUser_id(user1.getUser_id());
        BeanUtils.copyProperties(user, user1);
        userService.updateUser(user1);
        return "redirect:/main";
    }
}
